//
//  SDDetailsViewController.m
//  小胖兔旅游指南
//
//  Created by 小胖兔 on 15/4/15.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "SDDetailsViewController.h"
#import "AFNetworking.h"
#import "EScrollerView.h"
#import "PicViewController.h"
#import "AFNetworkTool.h"


@interface SDDetailsViewController ()<EScrollerViewDelegate>

@end

@implementation SDDetailsViewController

- (void)dealloc
{
    [_TwoID release];
    [_dic release];
    [_scrollView release];
    [_eScrollView release];
    [_aLabel release];
    [_bLabel release];
    [_conLabel release];
    [_picArray release];
    [_contentArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic = [NSMutableDictionary dictionary];
        self.picArray = [NSMutableArray array];
        self.contentArray = [NSMutableArray array];
    }
    return self;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setNature];
    [self requestData];
}

- (void)viewWillAppear:(BOOL)animated
{

    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    [self.navigationController.navigationBar setTranslucent:YES];
    
    [self.navigationController.navigationBar setAlpha:0.5];

    
    
}

- (void)setNature
{
    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen] bounds].size.width - 160, 44)];
    [aLabel setText:self.TwoName];
    [aLabel setTextAlignment:NSTextAlignmentCenter];
    [aLabel setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:aLabel];
}


- (void)requestData
{

    NSString *urlStr = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/%@", self.TwoID];
    NSDictionary *dic = @{@"app_version": @"1.9.0", @"device_type": @"1"};
    SDDetailsViewController *my = self;
    [AFNetworkTool JSONDataWithUrl:urlStr parameter:dic success:^(id json) {
        my.dic = json;
        [my dataToView];
    } fail:^{
        nil;
    }];
    
}

- (void)dataToView
{
    [self.navigationItem setTitle:[self.dic objectForKey:@"name"]];
    
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    
    [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 2)];
    [self.scrollView setScrollsToTop:YES];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];

    
    NSArray *array = [self.dic objectForKey:@"pictures"];
    for (NSDictionary *dict in array) {
        NSString *urlStr = [dict objectForKey:@"picture"];
        [self.picArray addObject:urlStr];
        NSString *contentA = [dict objectForKey:@"description"];
        [self.contentArray addObject:contentA];
    }
    
    self.eScrollView = [[EScrollerView alloc] initWithFrameRect:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, ([[UIScreen mainScreen] bounds].size.width / 998.0 * 554.0)) ImageArray:self.picArray TitleArray:nil];
    [self.eScrollView setDelegate:self];
    [self.eScrollView.pageControl setUserInteractionEnabled:NO];
    [self.scrollView addSubview:self.eScrollView];
    [self.eScrollView release];
    
    self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.eScrollView.frame.origin.y + self.eScrollView.frame.size.height + 10, [[UIScreen mainScreen] bounds].size.width - 20, self.eScrollView.frame.size.height / 3 / 2)];
    [self.scrollView addSubview:self.aLabel];
    [self.aLabel release];
    [self.aLabel setTextAlignment:NSTextAlignmentLeft];
    [self.aLabel setTextColor:[UIColor blackColor]];
    [self.aLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:26]];
    [self.aLabel setText:[self.dic objectForKey:@"name"]];
    
    self.bLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.aLabel.frame.origin.x, self.aLabel.frame.origin.y + self.aLabel.frame.size.height, self.aLabel.frame.size.width, self.aLabel.frame.size.height)];
    [self.scrollView addSubview:self.bLabel];
    [self.bLabel release];
    [self.bLabel setTextAlignment:NSTextAlignmentLeft];
    [self.bLabel setTextColor:[UIColor blackColor]];
    [self.bLabel setFont:[UIFont systemFontOfSize:16]];
    NSString *place = [NSString stringWithFormat:@"🏁 %@", [self.dic objectForKey:@"destination"]];
    [self.bLabel setText:place];
    
    UIView *lineHead = [[UIView alloc] initWithFrame:CGRectMake(0, self.bLabel.frame.origin.y +self.bLabel.frame.size.height, [[UIScreen mainScreen] bounds].size.width, 2)];
    [self.scrollView addSubview:lineHead];
    [lineHead release];
    [lineHead setBackgroundColor:[UIColor lightGrayColor]];
    
    
    NSString *content = [self.dic objectForKey:@"detail"];
    NSMutableDictionary *dicc = [NSMutableDictionary dictionary];
    [dicc setObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGRect rect = [content boundingRectWithSize:CGSizeMake(self.scrollView.frame.size.width - 20, 100000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicc context:nil];
    self.conLabel = [[UILabel alloc] initWithFrame:CGRectMake(lineHead.frame.origin.x + 10, lineHead.frame.origin.y + lineHead.frame.size.height + 20, lineHead.frame.size.width - 20, rect.size.height)];
    [self.scrollView addSubview:self.conLabel];
    [self.conLabel release];
    [self.conLabel setNumberOfLines:0];
    [self.conLabel setText:content];
    [self.conLabel setFont:[UIFont systemFontOfSize:17]];
    
    [self.scrollView setContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, self.conLabel.frame.origin.y + self.conLabel.frame.size.height + 50)];
    

}

#pragma mark -
#pragma EScrollView的协议方法（点击图片响应）
-(void)EScrollerViewDidClicked:(NSUInteger)index
{
    PicViewController *picView = [[PicViewController alloc] init];
    picView.pageNum = index;
    picView.PicArray = self.picArray;
    [picView.contentArray addObjectsFromArray: self.contentArray];
    [self.navigationController pushViewController:picView animated:YES];
    [picView release];
}

-(void)EScrollerViewDidPageChange:(NSUInteger)index
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
